<template>

  <div class="login">

 <el-form :model="userinfo" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="用户名" prop="username">
    <el-input   v-model.number = 'userinfo.username' clearable></el-input>
  </el-form-item>
  <el-form-item label="密码"   prop="password">
    <el-input type="password"   v-model.number = 'userinfo.password' clearable></el-input>
  </el-form-item>

  <el-form-item>
    <el-button type="primary" @click='login'>立即登录</el-button>

  </el-form-item>
</el-form>
  </div>
</template>

<script>
import { userlogin } from '../../api/api.js'

export default {
  data () {
    return {
      userinfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }

        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    login () {
      // validate 对整个表校验的方法  校验成功和失败的字段
      this.$refs.ruleForm.validate(val => {
        console.log(val)
        if (val) {
        // 调用接口
          userlogin(this.userinfo).then(res => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.$store.dispatch('changeUserInfo', res.list)
              this.$router.push('/home')
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
    .login{
        width: 100%;
        height: 100%;
        background-color: pink;
        display: flex;
        justify-content: center;
        align-items: center;
    }
</style>
